package com.oracle.cmp.service;


import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.Customer;
import com.oracle.cmp.entity.Parts;
import com.oracle.jdbc.util.Dao;
import org.apache.commons.httpclient.methods.multipart.Part;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class PartsServiceImpl implements PartsService{

	
	/**
	 * 新增客户
	 * @param parts
	 * @return
	 */

	public Result save(Parts parts) {
		//向客户表中增加一条记录；
		Dao.executeSql("ALTER TABLE biz_parts auto_increment=1");
		Dao.executeSql("insert into biz_parts values(default,?,?,?,?,?)",
				parts.getPartsname(),parts.getPartsmodel(),parts.getPartsloc(),parts.getPartsprodate(),parts.getPartsremark());
		return new Result(true,null,"保存成功");
	}

	
	/**
	 * 按客户名查询客户
	 * @return
	 */
	public Result queryForPage(Map<String,Object> map, int pageNum){
		String sql = "select * from biz_parts where 1=1";
		List params = new ArrayList();
		if(map.get("partsName")!=null&&!"".equals(map.get("partsName"))){
			sql += " and PartsName = ?";
			params.add(map.get("partsName"));
		}
		sql+=" limit ?,?";

		params.add((pageNum-1)*Constant.PAGESIZE);
		params.add(Constant.PAGESIZE);
		List<Parts> list = Dao.query(sql,Parts.class,params.toArray());
		return new Result(true,list,"查询成功");

	}
	/**
	 * 按id查询客户
	 */
	public Result selectById(int partsid) {
		Parts parts = Dao.queryOne("select * from biz_parts where PartsId = ?",Parts.class,partsid);
		return new Result(true,parts,"查询成功");
	}

	public Result update(Parts parts) {
		Dao.executeSql("update biz_parts set PartsName=?,PartsModel=?,PartsLoc=?,PartsProDate=?,PartsRemark=? where PartsId=?",
				parts.getPartsname(),parts.getPartsmodel(),parts.getPartsloc(),parts.getPartsprodate(),parts.getPartsremark(),parts.getPartsid());
		return new Result(true,null,"修改成功");
	}
	
	public Result delete(int partsid) {
		Dao.executeSql("delete from biz_parts where PartsId  = ?",partsid);
		return new Result(true,null,"删除成功");
	}
	public int count(Map<String,Object> map){
		String sql = "select count(0) from biz_parts where 1=1";
		List params = new ArrayList();
		if(map.get("partsName")!=null&&!"".equals(map.get("partsName"))){
			sql += " and partsName = ?";
			params.add(map.get("partsName"));
		}
		int total = ((Long)Dao.queryOne(sql,params.toArray())[0]).intValue();
		return total;
	}

	@Override
	public List<Parts> querajax(String partsname) {
		String sql = "select p.*,r.partsreqcount from biz_parts p left outer join biz_partsrepertory r on p.partsid=r.partsid where 1=1";
		List params = new ArrayList();
		if(partsname!=null&&!"".equals(partsname)){
			sql += " and partsname=?";
			params.add(partsname);
		}
		return Dao.query(sql,Parts.class,params.toArray());
	}
}
